<template>
  <div>
    <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef" label-width="100px" class="demo-ruleForm">
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="loginForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="loginForm.password" type='password'></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loginOnSubmit">登录</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getPhoneLogin } from 'network/login.js'
export default {
  name: 'loginMoreMode',
  data () {
    return {
      loginForm: {
        phone: '13479636407',
        password: 'kzj2372966718'
      },
      loginFormRules: {
        phone: [{ required: true, message: '请输入正确的手机号', trigger: 'blur' }],
        password: [{ required: true, message: '密码格式不对', trigger: 'blur' }]
      },
      userImg: '',
      currentUserInfo:
        window.localStorage.getItem('currentUserInfo') === 'null'
          ? null
          : JSON.parse(window.localStorage.getItem('currentUserInfo'))
    }
  },
  methods: {
    loginOnSubmit () {
      this.getPhoneLogin(this.loginForm.phone, this.loginForm.password)
      // this.$store.commit('loginVisible', false)
      this.$store.commit('loginimg', this.userImg)
    },
    getPhoneLogin (phone, password) {
      getPhoneLogin(phone, password).then((res) => {
        if (res === undefined) {
          this.$store.commit('loginVisible', true)
          this.$message.error('你的手机号未注册')
        }
        if (res.code !== 200) {
          this.$store.commit('loginVisible', true)
          this.$message.error('检查你的网络')
          return
        }
        console.log(res)
        document.cookie = res.cookie
        this.currentUserInfo = res.profile
        this.userImg = this.currentUserInfo.avatarUrl
      })
    }
  }
}
//  做到登录模块，但是头像没弄好
</script>
